package com.shushan.pages;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.net.bean.NetRetBean;
import com.android.net.core.common.RequestType;
import com.android.net.helper.NetHelper;
import com.android.net.listener.common.CallbackCode;
import com.android.net.url.UrlParse;
import com.shushan.base.BaseActivity;
import com.shushan.base.Constants;
import com.shushan.base.ScreenManager;
import com.shushan.network.UrlCommParse;
import com.shushan.network.UrlConst;
import com.shushan.network.listener.NetStrCommListener;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.shushanbao.R;
import com.shushan.shushanbao.wxapi.WXEntryActivity;
import com.shushan.util.ImageUtils;
import com.shushan.util.SharedPreferencesUtils;
import com.shushan.util.UmengEventUtil;
import com.shushan.util.WxApiUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * @ClassName: InviteActivity
 * @Desciption: //邀请界面
 * @author: zhangshihao
 * @date: 2018-04-09
 */
public class InviteActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = InviteActivity.class.getSimpleName();

    private ImageView ivShareFriend;
    private ImageView ivShareFriendsCircle;
    private TextView tvCancel;
    private RelativeLayout rlBtns;
    private ImageView ivInviteImg;
    private TextView tvChildName;
    private TextView tvInviteCode;
    private ImageView ivInviteFriend;
    private ImageView ivBack;
    private RelativeLayout rlBlackCover;

    private BabyApplication babyApplication = BabyApplication.getInstance();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invite);
        ScreenManager sm = new ScreenManager();
        sm.setStatusBar(true, this);
        initView();
//        getData();
        initData();
    }

    private void initView() {
        ivShareFriend = findViewById(R.id.iv_wechat_share_friend);
        ivShareFriendsCircle = findViewById(R.id.iv_wechat_share_friends_circle);
        tvCancel = findViewById(R.id.tv_cancel);
        rlBtns = findViewById(R.id.rl_btns);
        ivInviteImg = findViewById(R.id.iv_invite_img);

        ivShareFriend.setOnClickListener(this);
        ivShareFriendsCircle.setOnClickListener(this);
        tvCancel.setOnClickListener(this);
        tvChildName = findViewById(R.id.tv_child_name);
        tvInviteCode = findViewById(R.id.tv_invite_code);
        ivInviteFriend = findViewById(R.id.iv_invite_friend);
        ivInviteFriend.setOnClickListener(this);
        ivBack = findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        rlBlackCover = findViewById(R.id.rl_black_cover);
    }

    private void initData(){
        tvChildName.setText(getParentName());
        tvInviteCode.setText(babyApplication.getInviteCode());
    }

    private String getParentName(){
        StringBuffer sb = new StringBuffer();
        if (babyApplication.getCurrentChildBean() == null){
            return "";
        }
        String childName = babyApplication.getCurrentChildBean().childName;
        if(TextUtils.isEmpty(childName)){
            return "";
        }
        int nameLength = childName.length();
        if(nameLength == 1){
            sb.append(childName)
                    .append("的家长");
        }else if(nameLength > 1){
            sb.append(childName.charAt(0));
            for(int i=0;i<nameLength-1;i++){
                sb.append("x");
            }
            sb.append("的家长");
        }
        return sb.toString();
    }

    private void getData() {
        UrlParse urlParse = new UrlCommParse(this, UrlConst.BASE_URL).appendRegion(UrlConst.IMG_SHARE);
        UrlParse paramParse = new UrlCommParse(this)
                .putValue("phone", SharedPreferencesUtils.getStringDate(Constants.LOGIN_MOBILE_SHAREPREFERS_KEY));
        showWaitDialog();
        NetHelper.create()
                .url(urlParse.toStringOnlyHeader())
                .param(paramParse.toStringOnlyParam())
                .request(RequestType.REQUEST_TYPE_POST, new NetStrCommListener() {

                    @Override
                    protected void onCommon() {
                        dismissWaitDialog();
                    }

                    @Override
                    protected void onSuccess(String data) {
                        Log.i(TAG,"data : "+data);
                        try {
                            JSONObject jobj = new JSONObject(data);
                            String url = jobj.optString("url","");
                            if(!TextUtils.isEmpty(url)){
                                ImageUtils.loadWXImageView(InviteActivity.this, url, ivInviteImg);
                            }
                        }catch (JSONException je){
                            je.printStackTrace();
                        }

                    }

                    @Override
                    protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
                        Log.d(TAG, netRetBean.toString());
                        showNetError(netRetBean);
                    }
                });
    }

    @Override
    public void onClick(View v) {
        boolean result;
        switch (v.getId()) {
            case R.id.iv_wechat_share_friend:
                UmengEventUtil.onEvent(babyApplication,"Invite_firend_share_id","wechat");
                result = WxApiUtils.sharePic(this, WxApiUtils.SHARE_TYPE_SESSION, getScreenShotWithoutStatusBar());
                Log.d(TAG, "微信分享 = " + String.valueOf(result));
                break;
            case R.id.iv_wechat_share_friends_circle:
                UmengEventUtil.onEvent(babyApplication,"Invite_firend_share_id","circle");
                result = WxApiUtils.sharePic(this, WxApiUtils.SHARE_TYPE_TIME_LINE, getScreenShotWithoutStatusBar());
                Log.d(TAG, "微信分享 = " + String.valueOf(result));
                break;
            case R.id.tv_cancel:
                rlBtns.setVisibility(View.GONE);
                ivInviteFriend.setVisibility(View.VISIBLE);
                rlBlackCover.setVisibility(View.GONE);
                break;
            case R.id.iv_invite_friend:
                rlBtns.setVisibility(View.VISIBLE);
                ivInviteFriend.setVisibility(View.GONE);
                rlBlackCover.setVisibility(View.VISIBLE);

                WXEntryActivity.type = 3;
                break;
            case R.id.iv_back:
                onBackAction();
                break;
        }
    }

    //获取当前屏幕截图
    private Bitmap getScreenShotWithoutStatusBar() {
        rlBtns.setVisibility(View.GONE);
        ivBack.setVisibility(View.GONE);
        rlBlackCover.setVisibility(View.GONE);
        Bitmap bmp = ImageUtils.screenShotWithoutStatusBar(this);
        rlBtns.setVisibility(View.VISIBLE);
        ivBack.setVisibility(View.VISIBLE);
        rlBlackCover.setVisibility(View.VISIBLE);
        return bmp;
    }

}
